package com.sptpc.exam.controller;

import com.sptpc.exam.model.Student;
import com.sptpc.exam.service.IStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "http://localhost:3000", maxAge = 3600)
@Slf4j
@RestController
@RequestMapping("/stu")
public class StudentController {
    @Autowired
    IStudentService service;

//    @CrossOrigin
    @GetMapping("/")
    public List<Student> getAll(){
        return service.displayAll();
    }

//    @CrossOrigin
    @PostMapping("/add")
    public Student addNewStudent(@RequestBody Student student){
        service.addNewStudent(student);
        return student;
    }

//    @CrossOrigin
    @DeleteMapping("/del/{id}")
    public String delStudent(@PathVariable Long id){
        service.deleteStudent(id);
        return "删除成功";
    }

//    @CrossOrigin
    @PutMapping("/update")
    public Student modifyStudent(@RequestBody Student request){
        Student student = service.selectById(request.getId());
        BeanUtils.copyProperties(request,student);
        service.updateStudent(student);
        return student;
    }
}
